function warn(msg) {
    alert(msg);
}

function htmlEncode(value) {
    if (value) {
        return jQuery('<div />').text(value).html();
    } else {
        return '';
    }
}

function htmlDecode(value) {
    if (value) {
        return $('<div />').html(value).text();
    } else {
        return '';
    }
}

var DateConvertor = function (date) {
    this.d = new Date(date);

    this.fullYear = this.d.getFullYear();
    this.month = this.d.getMonth() + 1;
    this.day = this.d.getDate();
    this.hour = this.d.getHours();
    this.minute = this.d.getMinutes();
    this.second = this.d.getSeconds();

    this.toShortDate = function () {
        if (!date) return '';
        if (this.fullYear == new Date().getFullYear()) {
            return this.month + '月' + this.day + '日';
        }
        return this.fullYear + '年' + this.month + '月' + this.day + '日';
    }

    this.toShortTime = function () {
        if (!date) return '';
        return this.hour + ":" + this.minute + ":" + this.second;
    }

    this.toShortDateTime = function () {
        if (!date) return '';
        return this.toShortDate() + " " + this.toShortTime();
    }

    this.toFullDate = function () {
        if (!date) return '';
        return this.fullYear + '年' + this.month + '月' + this.day + '日';
    }

    this.toFullDateTime = function () {
        if (!date) return '';
        return this.toFullDate() + " " + this.toShortTime();
    }
}

function text2HTML(str) {
    return htmlEncode(str).replace(/\n/g, "<br />");
}

function createOperation(op, obj) {
    var oper = new Object();
    oper.operation = op;
    oper.jsonObject = obj || { 'na': 'na' };
    return oper;
}

function getJson(operation, successCallback, errorCallback) {
    $.ajax({
        method: 'POST',
        url: 'Gate',
        dataType: 'json',
        cache: false,
        data: { req: JSON.stringify(operation) },
        success: function (data) {
            if (successCallback) successCallback(data)
        },
        error: function (xhr, status, err) {
            if (errorCallback) errorCallback(status, err);
        }
    });
}

function getJsonSync(operation, successCallback, errorCallback) {
    $.ajax({
        async: false,
        method: 'POST',
        url: 'Gate',
        dataType: 'json',
        cache: false,
        data: { req: JSON.stringify(operation) },
        success: function (data) {
            if (successCallback) successCallback(data)
        },
        error: function (xhr, status, err) {
            if (errorCallback) errorCallback(status, err);
        }
    });
}

function userSelector(tree, jsonData) {
    var userTree = tree;
    var data = jsonData;
    var multiSelect = false;
    var selectedUsers = new Array();
    var filteredUsers = new Array();
    this.createNode = function (text, id, type) {
        var node = new Object();
        node.text = text;
        node.id = id;
        node.type = type;
        if (type == "team") {
            node.selectable = false;
            node.icon = 'glyphicon glyphicon-home';
        }
        else {
            if ($.inArray(id, this.selectedUsers) != -1) node.state = { selected: true };
            node.icon = 'glyphicon glyphicon-user';
        }
        return node;
    }

    this.fillTeamNodes = function (fillnode) {
        //alert ("subteam:"+JSON.stringify(fillnode.subTeams[0]));
        if ((fillnode.subTeams != undefined && fillnode.subTeams.length > 0) || (fillnode.users != undefined && fillnode.users.length > 0))
            fillnode.nodes = new Array();
        for (var u in fillnode.users) {
            if ($.inArray(fillnode.users[u].id, this.filteredUsers) == -1) {
                var n = this.createNode(fillnode.users[u].name, fillnode.users[u].id, 'user');
                if (fillnode.users[u].sex == "男") n.icon += " icon-male";
                if (fillnode.users[u].sex == "女") n.icon += " icon-female";
                fillnode.nodes.push(n);
            }
        }
        for (var t in fillnode.subTeams) {
            var n = this.createNode(fillnode.subTeams[t].name, fillnode.subTeams[t].id, 'team');
            n.subTeams = fillnode.subTeams[t].subTeams;
            n.users = fillnode.subTeams[t].users;
            this.fillTeamNodes(n);
            fillnode.nodes.push(n);
        }

        fillnode.subTeams = null;
        fillnode.users = null;
    }

    this.show = function () {
        if (this.data == undefined || this.data == null) return;
        var node = this.createNode(this.data.name, this.data.id, 'team');
        //alert("subteams:"+JSON.stringify(data.subTeams));
        node.subTeams = this.data.subTeams;
        node.users = this.data.users;
        this.fillTeamNodes(node);
        //alert("node:"+JSON.stringify(node));
        $("#" + this.userTree).treeview({
            data: node.nodes,
            showBorder: false,
            showTags: true,
            showIcon: true,
            multiSelect: this.multiSelect,
            showCheckbox: false,
            onNodeSelected: this.onNodeSelected,
            onNodeUnselected: this.onNodeUnselected
        });
    }

    this.getSelectedUsers = function () {
        return $("#" + this.userTree).treeview('getSelected');
    }

    this.getSelectedUserIds = function () {
        var nodesArr = $("#" + userTtree).treeview('getSelected');
        var uids = new Array();
        for (var i = 0; i < nodesArr.length; i++) {
            uids.push(nodesArr[i].id);
        }
        return uids;
    }
    this.setData = function (objTree) {
        this.data = objTree;
    }
    this.setUserTree = function (uTree) {
        this.userTree = uTree;
    }
    this.setSelectedUsers = function (uids) {
        this.selectedUsers = uids;
    }
    this.setFilteredUsers = function (uids) {
        this.filteredUsers = uids;
    }
    this.setMultiSelect = function (bMulti) {
        this.multiSelect = bMulti;
    }

    this.onNodeSelected;
    this.onNodeUnselected;
}